/**
 * Filename: TodayShowActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/9 2:57
 */
package com.github.postsanf.yinian.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.StealPhotosBigAdapter;
import com.github.postsanf.yinian.utils.UIHelper;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class StealPhotoBigShowActivity extends Activity {
    private ViewPager stealphoto_big_show;
    private StealPhotosBigAdapter mPagerAdapter;
    private LinearLayout stealphoto_big_indicator;
    private ArrayList<String> mDatas = new ArrayList<>();
    private List<ImageView> dots = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yn_activity_stealphotos_big);
        if (getIntent().getStringArrayListExtra("list") != null) {
            mDatas = getIntent().getStringArrayListExtra("list");
            int count = getIntent().getIntExtra("count", 0);
            switch (count) {
                case 1:
                    mDatas.remove(2);
                    mDatas.remove(1);
                    break;
                case 2:
                    mDatas.remove(2);
                    break;
            }
        } else {
            String url = getIntent().getStringExtra("url");
            mDatas.add(url);
        }
        initView();
    }

    private void initView() {
        stealphoto_big_show = (ViewPager) findViewById(R.id.stealphoto_big_show);
        stealphoto_big_indicator = (LinearLayout) findViewById(R.id.stealphoto_big_indicator);
        for (int i = 0; i < mDatas.size(); i++) {
            ImageView iv1 = new ImageView(this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            if (i != mDatas.size() - 1) {
                lp.rightMargin = UIHelper.dip2px(StealPhotoBigShowActivity.this, 20);
            }
            iv1.setLayoutParams(lp);
            iv1.setImageResource(R.drawable.stealphoto_big_gray);
            dots.add(iv1);
            stealphoto_big_indicator.addView(iv1);
        }
        mPagerAdapter = new StealPhotosBigAdapter(StealPhotoBigShowActivity.this, mDatas);
        stealphoto_big_show.setAdapter(mPagerAdapter);
        stealphoto_big_show.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                for (ImageView iv1 : dots) {
                    iv1.setImageResource(R.drawable.stealphoto_big_gray);
                }
                dots.get(position).setImageResource(R.drawable.stealphoto_big_white);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        if (getIntent() != null) {
            int pos = getIntent().getIntExtra("poistion", 0);
            stealphoto_big_show.setCurrentItem(pos);
        }
        findViewById(R.id.stealphoto_big_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
